
package edu.uhcl.csci4838.group3.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import edu.uhcl.csci4838.group3.R;
import edu.uhcl.csci4838.group3.view.CalendarActivity.CourseMeetingEntry;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	static final int FIRST_DAY_OF_WEEK = 0; // Sunday = 0, Monday = 1
	
	private Context mContext;
	
	private java.util.Calendar month;
	private Calendar selectedDate;
	private ArrayList<CalendarActivity.CourseMeetingEntry> dailyEntries;
	private ArrayList<ArrayList<CourseMeetingEntry>> monthlyEntries;
	
	public CalendarAdapter(Context c,
			Calendar monthCalendar) {
		month = monthCalendar;
		selectedDate = (Calendar) monthCalendar.clone();
		mContext = c;
		month.set(Calendar.DAY_OF_MONTH,
				1);
		this.dailyEntries = new ArrayList<CourseMeetingEntry>();
		this.monthlyEntries = new ArrayList<ArrayList<CourseMeetingEntry>>();
		refreshDays();
	}
	
	public void setMonthlyEntries(ArrayList<ArrayList<CourseMeetingEntry>> monthlyEntries) {
		this.monthlyEntries = monthlyEntries;
		Log.d("monthlyEntries.size()",
				""
						+ monthlyEntries.size());
	}
	
	// create a new view for each item referenced by the Adapter
	@Override
	public View getView(int position,
			View convertView,
			ViewGroup parent) {
		View v = convertView;
		TextView dayView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.calendar_item,
					null);
			
		}
		dayView = (TextView) v.findViewById(R.id.date);
		
		// disable empty days from the beginning
		if (days[position].equals("")) {
			dayView.setClickable(false);
			dayView.setFocusable(false);
		} else {
			// mark current day as focused
			if (month.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)
					&& month.get(Calendar.MONTH) == selectedDate.get(Calendar.MONTH)
					&& days[position].equals(""
							+ selectedDate.get(Calendar.DAY_OF_MONTH))) {
				v.setBackgroundResource(R.drawable.item_background_focused);
			} else {
				v.setBackgroundResource(R.drawable.list_item_background);
			}
		}
		dayView.setText(days[position]);
		
		// create date string for comparison
		String date = days[position];
		
		if (date.length() == 1) {
			date = "0"
					+ date;
		}
		String monthStr = ""
				+ (month.get(Calendar.MONTH) + 1);
		if (monthStr.length() == 1) {
			monthStr = "0"
					+ monthStr;
		}
		
		// get the icons and textview
		ImageView courseImage = (ImageView) v.findViewById(R.id.date_class_icon);
		ImageView holidayImage = (ImageView) v.findViewById(R.id.date_holiday_icon);
		TextView meetingsTodayText = (TextView) v.findViewById(R.id.courseMeetings);
		
		// Set the icons visibilities according to the meeting entries from each
		// day
		if (date.length() > 0) {
			
			dailyEntries = monthlyEntries.get(Integer.parseInt(days[position]) - 1);
			
			if (dailyEntries.size() > 0) {
				
				// Check to see if holiday with no course meetings
				if (dailyEntries.size() == 1
						&& dailyEntries.get(0)
								.getIsHoliday() == true
						&& dailyEntries.get(0)
								.getId() == -1) {
					
					courseImage.setVisibility(View.GONE);
					holidayImage.setVisibility(View.VISIBLE);
					meetingsTodayText.setText("");
					
				} else {
					boolean isAllHolidays = true;
					
					// Check to see if today is a holiday without any overrides
					for (int k = 0; k < dailyEntries.size(); k++) {
						if (dailyEntries.get(k)
								.getIsHoliday() == false) {
							isAllHolidays = false;
							break;
						}
					}
					
					if (isAllHolidays) {
						
						courseImage.setVisibility(View.GONE);
						holidayImage.setVisibility(View.VISIBLE);
						
						String s = "";
						for (int k = 0; k < dailyEntries.size(); k++) {
							
							CourseMeetingEntry entry = dailyEntries.get(k);
							s += entry.getId()
									+ "\n"
									+ entry.getCourseName()
									+ "\n"
									+ entry.getHolidayName()
									+ "\n";
							
						}
						
						meetingsTodayText.setText(s);
						
					} else {
						
						courseImage.setVisibility(View.VISIBLE);
						holidayImage.setVisibility(View.GONE);
						
						String s = "";
						for (int k = 0; k < dailyEntries.size(); k++) {
							
							CourseMeetingEntry entry = dailyEntries.get(k);
							
							if (entry.getIsHoliday() == true) {
								s += entry.getId()
										+ "\n"
										+ entry.getCourseName()
										+ "\n"
										+ entry.getHolidayName()
										+ "\n";
							} else {
								
								String tempString = (entry.getStartTimeHour() > 12
										? ""
												+ (entry.getStartTimeHour() - 12)
										: ""
												+ entry.getStartTimeHour())
										+ ":"
										+ (entry.getStartTimeMinute() < 10
												? "0"
														+ entry.getStartTimeMinute()
												: entry.getStartTimeMinute())
										+ (entry.getStartTimeHour() > 12
												? " PM"
												: " AM")
										+ " - "
										+ (entry.getEndTimeHour() > 12
												? ""
														+ (entry.getEndTimeHour() - 12)
												: ""
														+ entry.getEndTimeHour())
										+ ":"
										+ (entry.getEndTimeMinute() < 10
												? "0"
														+ entry.getEndTimeMinute()
												: entry.getEndTimeMinute())
										+ (entry.getEndTimeHour() > 12
												? " PM"
												: " AM");
								
								s += entry.getId()
										+ "\n"
										+ entry.getCourseName()
										+ "\n"
										+ tempString
										+ "\n";
							}
							
						}
						
						meetingsTodayText.setText(s);
						
					}
				}
				
			} else {
				
				courseImage.setVisibility(View.INVISIBLE);
				holidayImage.setVisibility(View.GONE);
				meetingsTodayText.setText("");
			}
			
		} else {
			
			courseImage.setVisibility(View.INVISIBLE);
			holidayImage.setVisibility(View.GONE);
			
		}
		return v;
	}
	
	public int getCount() {
		return days.length;
	}
	
	public Object getItem(int position) {
		return null;
	}
	
	public long getItemId(int position) {
		return 0;
	}
	
	public void refreshDays() {
		
		dailyEntries.clear();
		monthlyEntries.clear();
		
		int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
		int firstDay = (int) month.get(Calendar.DAY_OF_WEEK);
		
		// figure size of the array
		if (firstDay == 1) {
			days = new String[lastDay
					+ (FIRST_DAY_OF_WEEK * 6)];
		} else {
			days = new String[lastDay
					+ firstDay
					- (FIRST_DAY_OF_WEEK + 1)];
		}
		
		int j = FIRST_DAY_OF_WEEK;
		
		// populate empty days before first real day
		if (firstDay > 1) {
			for (j = 0; j < firstDay
					- FIRST_DAY_OF_WEEK; j++) {
				days[j] = "";
			}
		} else {
			for (j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) {
				days[j] = "";
			}
			j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7
		}
		
		// populate days
		int dayNumber = 1;
		for (int i = j - 1; i < days.length; i++) {
			days[i] = ""
					+ dayNumber;
			dayNumber++;
		}
	}
	
	// references to our items
	public String[] days;
}
